package day3;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;

//测试序列化
public class TestSerializable {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        //output();
        input();
    }
    //反序列化
    public static void input() throws IOException, ClassNotFoundException {
        FileInputStream fis=new FileInputStream("d://io//student.xxx");
        ObjectInputStream ois=new ObjectInputStream(fis);
        Student stu=(Student)ois.readObject();
        System.out.println(stu.getName()+" "+stu.getStuno()+" "+stu.getScore());
        System.out.println(stu.getInfo().getPhone());
    }
    //序列化
    public static void output() throws IOException {
        Student s=new Student();
        s.setName("建勇");
        s.setStuno("001");;
        s.setScore(89);
        StudentInfo info=new StudentInfo();
        info.setStuno("001");
        info.setPhone("110");
        s.setInfo(info);
        //因为序列化是转换成字节序列 所以文件内容的不能修改
        //所以本地文件会设置一个 无法打开的后缀名
        FileOutputStream fos=
                new FileOutputStream("d://io//student.xxx");
        ObjectOutputStream oos=new ObjectOutputStream(fos);
        oos.writeObject(s);  //student(info)-->List<Student>
    }
}
//序列化必须要实现该接口 否则报错
class Student implements Serializable {
    //序列化还会默认的唯一标识:作用用于检测序列化和反序列化是否是同一个ID
    private static final long serialVersionUID=12345678913242342L;

    private String stuno;//学号
    //忽略序列化 属性的值不保存
    private static String name;//姓名
    //忽略序列化 属性的值不保存
    private transient double score;//分数
    private StudentInfo info;
    //alt+insert 快速生成get和set
    public StudentInfo getInfo() {
        return info;
    }
    public void setInfo(StudentInfo info) {
        this.info = info;
    }

    public String getStuno() {
        return stuno;
    }

    public void setStuno(String stuno) {
        this.stuno = stuno;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getScore() {
        return score;
    }

    public void setScore(double score) {
        this.score = score;
    }
}
class StudentInfo implements Serializable{
    private String stuno;
    private String sex;
    private String phone;

    public String getStuno() {
        return stuno;
    }

    public void setStuno(String stuno) {
        this.stuno = stuno;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }
}